package com.didispace.web;

import com.didispace.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class ConsumerController {

    @Autowired
    HelloService helloService;

    // p75 RestTemplate 详解
    @RequestMapping(value = "/restTemplate", method = RequestMethod.GET)
    public String restTemplateConsumer() {
        return helloService.restTemplateService();
    }

    @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
    public String helloConsumer() {
        return helloService.helloService();
    }

    @RequestMapping(value = "/getUser1/{name}", method = RequestMethod.GET)
    public User user1(@PathVariable String name) {
        return new User("Garbo", 26);
    }

    @RequestMapping(value = "/getUser2/{name}", method = RequestMethod.GET)
    public String user2(@PathVariable String name) {
        return helloService.user2(name);
    }

    @RequestMapping(value = "/getUser3/{name}", method = RequestMethod.GET)
    @ResponseBody
    private User restfulGetId(@PathVariable String name) {
        return helloService.user3(name);
    }

}

// 2022-04-29 16:59:44.147  INFO 20012 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_RIBBON-CONSUMER/localhost:ribbon-consumer:9000 - registration status: 204

// http://localhost:9000/restTemplate
// Hello World

// GET 请求 第一种：getForEntity 函数。
// didi
// 而返回的 ResponseEntity对象中的 body 内容类型会根据第二个参数转换为 String 类型
// 返回的 body 是一个 User 对象类型
// name=didi, age=null
// name=didi, age=17
// 2. 使用 Map 类型
// dada
// 3. 使用 getForEntity(URI url, Class responseType)
// dodo
// GET 请求 第二种：getForObject 函数。
// p77 运行失败
// POST 请求 第一种：postForEntity 函数。
// didi
// POST 请求 第二种：postForObject 函数。
// didi
// POST 请求 第三种：postForLocation 函数。
// null
// PUT 请求。
// DELETE 请求。

// 2022-04-29 17:00:29.924  INFO 20012 --- [nio-9000-exec-1] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client HELLO-SERVICE initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=HELLO-SERVICE,current list of Servers=[localhost:8081],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone;	Instance count:1;	Active connections count: 0;	Circuit breaker tripped count: 0;	Active connections per server: 0.0;]
// },Server stats: [[Server:localhost:8081;	Zone:defaultZone;	Total Requests:0;	Successive connection failure:0;	Total blackout seconds:0;	Last connection made:Thu Jan 01 08:00:00 CST 1970;	First connection made: Thu Jan 01 08:00:00 CST 1970;	Active Connections:0;	total failure count in last (1000) msecs:0;	average resp time:0.0;	90 percentile resp time:0.0;	95 percentile resp time:0.0;	min resp time:0.0;	max resp time:0.0;	stddev resp time:0.0]
// ]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@2c6f6bbc
// 2022-04-29 17:00:30.914  INFO 20012 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty  : Flipping property: HELLO-SERVICE.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647

// Spring RestTemplate中常用的请求方式
// https://blog.csdn.net/MrRightMeng/article/details/79741838
// restTemplate getForObject中map传参问题
// https://www.cnblogs.com/zhengy-001/p/11694055.html

// http://localhost:9000/getUser1/Garbo
// {"name":"Garbo","age":26}

// http://localhost:9000/getUser2/Garbo
// {"name":"Garbo","age":26}

// http://localhost:9000/getUser3/Garbo
// {"name":"Garbo","age":26}